﻿

//

using LaserMark.Models;
using System.Text;

namespace LaserMark.Extensions;
public static class SvgBuilderExtension
{
    public static string Create(Original original)
    {
        StringBuilder builder = new();
        builder.Append(
            $"<svg width=\"{original.Width}\" height=\"{original.Height}\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:svg=\"http://www.w3.org/2000/svg\"  style=\"border: 1px solid steelblue\">");

        foreach (var point in original.MarkPoints)
        {
            string group = "";

            if (point.Type == "OPT")
            {
                double x;
                double y;
                if (point.IsY)
                {
                    x = point.X - point.SheetWidth;
                    y = point.Y - point.SheetHeight;

                    group = $"""
                         <g>
                             <rect x="{x}" y="{y}" width="{point.SheetWidth}" height="{point.SheetHeight}" fill="#2580b4" style="stroke:black;stroke-width:2;" />
                             <text x="{x + point.SheetWidth / 3}" y="{y}" fill="#fff" class="temp-text">{point.SheetWidth}</text>
                             <g transform="rotate(90 {x} {y + point.SheetHeight / 3})">
                                 <text x="{x}" y="{y + point.SheetHeight / 3}" fill="#fff" class="temp-text">{point.SheetHeight}</text>
                             </g>
                         </g>
                         """;
                }
                else
                {
                    x = point.X - point.SheetHeight;
                    y = point.Y - point.SheetWidth;

                    group = $"""
                         <g>
                             <rect x="{x}" y="{y}" width="{point.SheetHeight}" height="{point.SheetWidth}" fill="#2580b4" style="stroke:black;stroke-width:2;" />
                             <text x="{x + point.SheetHeight / 3}" y="{y}" fill="#fff" class="temp-text">{point.SheetHeight}</text>
                             <g transform="rotate(90 {x} {y + point.SheetWidth / 3})">
                                 <text x="{x}" y="{y + point.SheetWidth / 3}" fill="#fff" class="temp-text">{point.SheetWidth}</text>
                             </g>
                         </g>
                         """;
                }
            }
            else
            {
                group = $"""
                         <g>
                             <rect x="{point.X}" y="{point.Y}" width="{point.XArrow}" height="{point.YArrow}" fill="#2580b4" style="stroke:black;stroke-width:2;" />
                             <text x="{point.X + point.XArrow / 3}" y="{point.Y}" fill="#fff" class="temp-text">{point.XArrow}</text>
                             <g transform="rotate(90 {point.X} {point.Y + point.YArrow / 3})">
                                 <text x="{point.X}" y="{point.Y + point.XArrow / 3}" fill="#fff" class="temp-text">{point.YArrow}</text>
                             </g>
                         </g>
                         """;
            }

            builder.Append(group);
        }
        builder.Append("</svg>");

        return builder.ToString();
    }
}
